home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / tsipp / tsipp.lha / tsipp3.0a / src / tSippMisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-02  |  12.9 KB  |  406 lines

  1. /*
  2.  *=============================================================================
  3.  *                                  tSippMisc.c
  4.  *-----------------------------------------------------------------------------
  5.  * Miscellaneous commands.
  6.  *-----------------------------------------------------------------------------
  7.  * Copyright 1992 Mark Diekhans
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies.  Mark Diekhans makes
  11.  * no representations about the suitability of this software for any purpose.
  12.  * It is provided "as is" without express or implied warranty.
  13.  *-----------------------------------------------------------------------------
  14.  * $Id: tSippMisc.c,v 2.0 1992/11/02 03:56:23 markd Rel $
  15.  *============================================================================
  16.  */
  17.  
  18. #include "tSippInt.h"
  19. #include "patchlevel.h"
  20.  
  21. /*
  22.  * Version string containing full version (SIPP version, TSIPP suffix and maybe
  23.  * patchlevel.
  24.  */
  25. char *TSIPP_VERSION;
  26.  
  27. /*
  28.  * Implied column 3 of the Transf_mat.
  29.  */
  30. static double matrixCol3 [] = {0.0, 0.0, 0.0, 1.0};
  31.  
  32. /*=============================================================================
  33.  * SippMkVector --
  34.  *   Implements the command:
  35.  *     SippMkVector {pt0_x pt0_y pt0_z} {pt1_x pt1_y pt1_z}
  36.  * Note:
  37.  *   This procedure has standard Tcl command calling sematics.  ClientData
  38.  * contains a pointer to the Tcl SIPP global structure.
  39.  *-----------------------------------------------------------------------------
  40.  */
  41. static int
  42. SippMkVector (clientData, interp, argc, argv)
  43.     char       *clientData;
  44.     Tcl_Interp *interp;
  45.     int         argc;
  46.     char      **argv;
  47. {
  48.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  49.     Vector        point0, point1, vector;
  50.  
  51.     if (argc != 3) {
  52.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  53.                           " {pt0_x pt0_y pt0_z} {pt1_x pt1_y pt1_z}",
  54.                           (char *) NULL);
  55.         return TCL_ERROR;
  56.     }                     
  57.  
  58.     if (!TSippConvertVertex (tSippGlobPtr, argv [1], &point0))
  59.         return TCL_ERROR;
  60.     if (!TSippConvertVertex (tSippGlobPtr, argv [2], &point1))
  61.         return TCL_ERROR;
  62.     
  63.     vector.x = point1.x - point0.x;
  64.     vector.y = point1.y - point0.y;
  65.     vector.z = point1.z - point0.z;
  66.     sprintf (tSippGlobPtr->interp->result, "%g %g %g",
  67.              vector.x, vector.y, vector.z);
  68.     return TCL_OK;
  69.  
  70. } /* SippMkVector */
  71.  
  72. /*=============================================================================
  73.  * SippMatMult --
  74.  *   Implements the command:
  75.  *     SippMatMult matrix1 matrix2
  76.  * Note:
  77.  *   This procedure has standard Tcl command calling sematics.  ClientData
  78.  * contains a pointer to the Tcl SIPP global structure.
  79.  *-----------------------------------------------------------------------------
  80.  */
  81. static int
  82. SippMatMult (clientData, interp, argc, argv)
  83.     char       *clientData;
  84.     Tcl_Interp *interp;
  85.     int         argc;
  86.     char      **argv;
  87. {
  88.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  89.     Transf_mat    matrix1, matrix2, resultMat;
  90.     int           row, col;
  91.  
  92.     if (argc != 3) {
  93.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  94.                           " matrix1 matrix2", (char *) NULL);
  95.         return TCL_ERROR;
  96.     }
  97.     if (!TSippConvertMatrix (tSippGlobPtr, argv [1], &matrix1))
  98.         return TCL_ERROR;
  99.     if (!TSippConvertMatrix (tSippGlobPtr, argv [2], &matrix2))
  100.         return TCL_ERROR;
  101.  
  102.     for (row=0; row <= 3; row++)
  103.       for (col=0; col <= 2; col++)
  104.         resultMat.mat [row][col] = 
  105.             matrix1.mat [row][0] * matrix2.mat [0][col] +
  106.             matrix1.mat [row][1] * matrix2.mat [1][col] +
  107.             matrix1.mat [row][2] * matrix2.mat [2][col] +
  108.             matrixCol3  [row]    * matrix2.mat [3][col];
  109.     
  110.     Tcl_SetResult (interp, TSippFormatMatrix (&resultMat), TCL_DYNAMIC);
  111.     return TCL_OK;
  112.  
  113. } /* SippMatMult */
  114.  
  115. /*=============================================================================
  116.  * SippShowBackFaces --
  117.  *   Implements the command:
  118.  *     SippShowBackFaces [flag]
  119.  * Note:
  120.  *   This procedure has standard Tcl command calling sematics.  ClientData
  121.  * contains a pointer to the Tcl SIPP global structure.
  122.  *-----------------------------------------------------------------------------
  123.  */
  124. static int
  125. SippShowBackFaces (clientData, interp, argc, argv)
  126.     char       *clientData;
  127.     Tcl_Interp *interp;
  128.     int         argc;
  129.     char      **argv;
  130. {
  131.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  132.     int           flag;
  133.  
  134.     if (argc > 2) {
  135.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  136.                           " [flag]", (char *) NULL);
  137.         return TCL_ERROR;
  138.     }                     
  139.     if (argc == 1) {
  140.         if (tSippGlobPtr->showBackFaces)
  141.             interp->result = "1";
  142.         else
  143.             interp->result = "0";
  144.     } else {
  145.         if (Tcl_GetBoolean (interp, argv [1], &flag) != TCL_OK)
  146.             return TCL_ERROR;
  147.         tSippGlobPtr->showBackFaces = flag;
  148.         sipp_show_backfaces (flag);
  149.     }
  150.     return TCL_OK;
  151.  
  152. } /* SippShowBackFaces */
  153.  
  154. /*=============================================================================
  155.  * SippBackground --
  156.  *   Implements the command:
  157.  *     SippBackground [color]
  158.  * Note:
  159.  *   This procedure has standard Tcl command calling sematics.  ClientData
  160.  * contains a pointer to the Tcl SIPP global structure.
  161.  *-----------------------------------------------------------------------------
  162.  */
  163. static int
  164. SippBackground (clientData, interp, argc, argv)
  165.     char       *clientData;
  166.     Tcl_Interp *interp;
  167.     int         argc;
  168.     char      **argv;
  169. {
  170.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  171.     Color         color;
  172.  
  173.     if (argc > 2) {
  174.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  175.                           " [color]", (char *) NULL);
  176.         return TCL_ERROR;
  177.     }
  178.     if (argc == 1) {
  179.         sprintf (interp->result, "%g %g %g",
  180.                  tSippGlobPtr->backgroundColor.red,
  181.                  tSippGlobPtr->backgroundColor.grn,
  182.                  tSippGlobPtr->backgroundColor.blu);
  183.     } else {
  184.         if (!TSippConvertColor (tSippGlobPtr, argv [1], &color))
  185.             return TCL_ERROR;
  186.         sipp_background (color.red, color.grn, color.blu);
  187.         tSippGlobPtr->backgroundColor = color;
  188.     }
  189.     return TCL_OK;
  190.  
  191. } /* SippBackground */
  192.  
  193. /*=============================================================================
  194.  * SippLineColor --
  195.  *   Implements the command:
  196.  *     SippLineColor [color]
  197.  * Note:
  198.  *   This procedure has standard Tcl command calling sematics.  ClientData
  199.  * contains a pointer to the Tcl SIPP global structure.
  200.  *-----------------------------------------------------------------------------
  201.  */
  202. static int
  203. SippLineColor (clientData, interp, argc, argv)
  204.     char       *clientData;
  205.     Tcl_Interp *interp;
  206.     int         argc;
  207.     char      **argv;
  208. {
  209.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  210.     Color         color;
  211.  
  212.     if (argc > 2) {
  213.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  214.                           " [color]", (char *) NULL);
  215.         return TCL_ERROR;
  216.     }
  217.     if (argc == 1) {
  218.         sprintf (interp->result, "%g %g %g",
  219.                  tSippGlobPtr->lineColor.red,
  220.                  tSippGlobPtr->lineColor.grn,
  221.                  tSippGlobPtr->lineColor.blu);
  222.     } else {
  223.         if (!TSippConvertColor (tSippGlobPtr, argv [1], &color))
  224.             return TCL_ERROR;
  225.         tSippGlobPtr->lineColor = color;
  226.     }
  227.     return TCL_OK;
  228.  
  229. } /* SippLineColor */
  230.  
  231. /*=============================================================================
  232.  * SippShadows --
  233.  *   Implements the command:
  234.  *     SippShadows flag [size]
  235.  * Note:
  236.  *   This procedure has standard Tcl command calling sematics.  ClientData
  237.  * contains a pointer to the Tcl SIPP global structure.
  238.  *-----------------------------------------------------------------------------
  239.  */
  240. static int
  241. SippShadows (clientData, interp, argc, argv)
  242.     char       *clientData;
  243.     Tcl_Interp *interp;
  244.     int         argc;
  245.     char      **argv;
  246. {
  247.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  248.     int           flag;
  249.     unsigned      size;
  250.  
  251.     if ((argc < 2) || (argc > 3)) {
  252.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  253.                           " flag [size]", (char *) NULL);
  254.         return TCL_ERROR;
  255.     }                     
  256.     if (Tcl_GetBoolean (interp, argv [1], &flag) != TCL_OK)
  257.         return TCL_ERROR;
  258.     if (flag) {
  259.         if (argc != 3) {
  260.             Tcl_AppendResult (interp, "size argument must be specified when ",
  261.                               "enabling shadows", (char *) NULL);
  262.             return TCL_ERROR;
  263.         }
  264.         if (!TSippConvertPosUnsigned (tSippGlobPtr, argv [2], &size))
  265.             return TCL_ERROR;
  266.     } else {
  267.         if (argc != 2) {
  268.             Tcl_AppendResult (interp, "size argument must be omitted when ",
  269.                               "disabling shadows", (char *) NULL);
  270.             return TCL_ERROR;
  271.         }
  272.         size = 0;
  273.     }
  274.     sipp_shadows (flag, size);
  275.     return TCL_OK;
  276.  
  277. } /* SippShadows */
  278.  
  279. /*=============================================================================
  280.  * SippInfo --
  281.  *   Implements the command:
  282.  *     SippInfo attribute
  283.  * Note:
  284.  *   This procedure has standard Tcl command calling sematics.  ClientData
  285.  * contains a pointer to the Tcl SIPP global structure.
  286.  *-----------------------------------------------------------------------------
  287.  */
  288. static int
  289. SippInfo (clientData, interp, argc, argv)
  290.     char       *clientData;
  291.     Tcl_Interp *interp;
  292.     int         argc;
  293.     char      **argv;
  294. {
  295.     tSippGlob_pt  tSippGlobPtr = (tSippGlob_pt) clientData;
  296.  
  297.     if (argc != 2) {
  298.         Tcl_AppendResult (interp, "wrong # args: ", argv [0],
  299.                           " attribute", (char *) NULL);
  300.         return TCL_ERROR;
  301.     }                     
  302.     if (STREQU ("version", argv [1])) {
  303.         interp->result = TSIPP_VERSION;
  304.         return TCL_OK;
  305.     }
  306.     if (STREQU ("sippversion", argv [1])) {
  307.         interp->result = SIPP_VERSION;
  308.         return TCL_OK;
  309.     }
  310.     if (STREQU ("tsippversion", argv [1])) {
  311.         interp->result = TSIPP_VERSION_SUFFIX;
  312.         return TCL_OK;
  313.     }
  314.     if (STREQU ("tsipppatchlevel", argv [1])) {
  315.         sprintf (interp->result, "%d", PATCHLEVEL);
  316.         return TCL_OK;
  317.     }
  318.     if (STREQU ("rle", argv [1])) {
  319. #ifdef TSIPP_NO_RLE
  320.         interp->result = "0";
  321. #else
  322.         interp->result = "1";
  323. #endif
  324.         return TCL_OK;
  325.     }
  326.  
  327.     Tcl_AppendResult (interp, "expected an attribute of \"version\", ",
  328.                       "\"sippversion\", \"tsippversion\", ",
  329.                       "\"tsipppatchlevel\", or \"rle\", got \"",
  330.                       argv [1], "\"", (char *) NULL);
  331.     return TCL_ERROR;
  332.  
  333. } /* SippInfo */
  334.  
  335. /*=============================================================================
  336.  * TSippInitialize -- 
  337.  *   Initialize the Tcl/Sipp environment.
  338.  * Parameters:
  339.  *   o interp (I) - Pointer to the Tcl interpreter.
  340.  *-----------------------------------------------------------------------------
  341.  */
  342. void
  343. TSippInitialize (interp)
  344.     Tcl_Interp *interp;
  345. {
  346.     static tSippTclCmdTbl_t cmdTable [] = {
  347.         {"SippMkVector",      SippMkVector},
  348.         {"SippMatMult",       SippMatMult},
  349.         {"SippShowBackFaces", SippShowBackFaces},
  350.         {"SippBackground",    SippBackground},
  351.         {"SippLineColor",     SippLineColor},
  352.         {"SippShadows",       SippShadows},
  353.         {"SippInfo",          SippInfo},
  354.         {NULL,                NULL}
  355.     };
  356.     tSippGlob_pt tSippGlobPtr;
  357.     char         patchBuf [10];
  358.  
  359.    /*
  360.     * Define strings to be returned by the infox command.  Include patch
  361.     * level if its not zero.
  362.     */
  363.  
  364.     tclAppName = "tSIPP";
  365.     tclAppLongname = "Tcl-SIPP";
  366.     TSIPP_VERSION = ckalloc (strlen (SIPP_VERSION) +
  367.                              strlen (TSIPP_VERSION_SUFFIX) + 10);
  368.     tclAppVersion = TSIPP_VERSION; 
  369.     strcpy (TSIPP_VERSION, SIPP_VERSION);
  370.     strcat (TSIPP_VERSION, TSIPP_VERSION_SUFFIX);
  371.  
  372. #if PATCHLEVEL
  373.     sprintf (patchBuf, "-P%d", PATCHLEVEL);
  374.     strcat (TSIPP_VERSION, patchBuf);
  375. #endif
  376.  
  377.     sipp_init ();
  378.  
  379.     tSippGlobPtr = (tSippGlob_pt) ckalloc (sizeof (tSippGlob_t));
  380.  
  381.     tSippGlobPtr->interp = interp;
  382.     tSippGlobPtr->showBackFaces = FALSE;
  383.  
  384.     tSippGlobPtr->backgroundColor.red = 0.0;
  385.     tSippGlobPtr->backgroundColor.grn = 0.0;
  386.     tSippGlobPtr->backgroundColor.blu = 0.0;
  387.  
  388.     tSippGlobPtr->lineColor.red = 1.0;
  389.     tSippGlobPtr->lineColor.grn = 1.0;
  390.     tSippGlobPtr->lineColor.blu = 1.0;
  391.  
  392.     TSippInitCmds (tSippGlobPtr, cmdTable);
  393.  
  394.     TSippBezierInit (tSippGlobPtr);
  395.     TSippShaderInit (tSippGlobPtr);
  396.     TSippPolyInit   (tSippGlobPtr);
  397.     TSippObjectInit (tSippGlobPtr);
  398.     TSippLightInit  (tSippGlobPtr);
  399.     TSippCameraInit (tSippGlobPtr);
  400.     TSippPrimInit   (tSippGlobPtr);
  401.     TSippPPMInit    (tSippGlobPtr);
  402.     TSippRLEInit    (tSippGlobPtr);
  403.  
  404.  
  405. } /* TSippInitialize */
  406.